﻿namespace Pub.Class
{
    using Microsoft.Win32;
    using System;

    public static class Registry2
    {
        public static void Delete(string path, string valueName)
        {
            Delete(RegistryEnum.LocalMachine, path, valueName);
        }

        public static void Delete(RegistryEnum registryEnum, string path, string valueName)
        {
            RegistryKey localMachine = null;
            switch (registryEnum)
            {
                case RegistryEnum.LocalMachine:
                    localMachine = Registry.LocalMachine;
                    break;

                case RegistryEnum.ClassesRoot:
                    localMachine = Registry.ClassesRoot;
                    break;

                case RegistryEnum.CurrentConfig:
                    localMachine = Registry.CurrentConfig;
                    break;

                case RegistryEnum.CurrentUser:
                    localMachine = Registry.CurrentUser;
                    break;

                case RegistryEnum.DynData:
                    localMachine = Registry.DynData;
                    break;

                case RegistryEnum.PerformanceData:
                    localMachine = Registry.PerformanceData;
                    break;

                case RegistryEnum.Users:
                    localMachine = Registry.Users;
                    break;
            }
            string[] strArray = path.Trim(new char[] { '\\' }).Split(new char[] { '\\' });
            if (!strArray.IsNull() && (strArray.Length != 0))
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    localMachine = localMachine.OpenSubKey(strArray[i], true);
                    if (localMachine.IsNull())
                    {
                        return;
                    }
                    if (i == (strArray.Length - 1))
                    {
                        localMachine.DeleteValue(valueName, false);
                    }
                }
            }
        }

        public static bool Exists(string path, string key)
        {
            return Exists(RegistryEnum.LocalMachine, path, key);
        }

        public static bool Exists(RegistryEnum registryEnum, string path, string key)
        {
            RegistryKey key2 = null;
            switch (registryEnum)
            {
                case RegistryEnum.LocalMachine:
                    key2 = Registry.LocalMachine.OpenSubKey(path, false);
                    break;

                case RegistryEnum.ClassesRoot:
                    key2 = Registry.ClassesRoot.OpenSubKey(path, false);
                    break;

                case RegistryEnum.CurrentConfig:
                    key2 = Registry.CurrentConfig.OpenSubKey(path, false);
                    break;

                case RegistryEnum.CurrentUser:
                    key2 = Registry.CurrentUser.OpenSubKey(path, false);
                    break;

                case RegistryEnum.DynData:
                    key2 = Registry.DynData.OpenSubKey(path, false);
                    break;

                case RegistryEnum.PerformanceData:
                    key2 = Registry.PerformanceData.OpenSubKey(path, false);
                    break;

                case RegistryEnum.Users:
                    key2 = Registry.Users.OpenSubKey(path, false);
                    break;
            }
            string[] subKeyNames = key2.GetSubKeyNames();
            for (int i = 0; i < subKeyNames.Length; i++)
            {
                if (key.Equals(subKeyNames[i]))
                {
                    return true;
                }
            }
            return false;
        }

        public static object Read(string path, string valueName)
        {
            return Read(RegistryEnum.LocalMachine, path, valueName);
        }

        public static object Read(RegistryEnum registryEnum, string path, string valueName)
        {
            RegistryKey localMachine = null;
            switch (registryEnum)
            {
                case RegistryEnum.LocalMachine:
                    localMachine = Registry.LocalMachine;
                    break;

                case RegistryEnum.ClassesRoot:
                    localMachine = Registry.ClassesRoot;
                    break;

                case RegistryEnum.CurrentConfig:
                    localMachine = Registry.CurrentConfig;
                    break;

                case RegistryEnum.CurrentUser:
                    localMachine = Registry.CurrentUser;
                    break;

                case RegistryEnum.DynData:
                    localMachine = Registry.DynData;
                    break;

                case RegistryEnum.PerformanceData:
                    localMachine = Registry.PerformanceData;
                    break;

                case RegistryEnum.Users:
                    localMachine = Registry.Users;
                    break;
            }
            string[] strArray = path.Trim(new char[] { '\\' }).Split(new char[] { '\\' });
            if (!strArray.IsNull() && (strArray.Length != 0))
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    localMachine = localMachine.OpenSubKey(strArray[i]);
                    if (localMachine.IsNull())
                    {
                        return null;
                    }
                    if (i == (strArray.Length - 1))
                    {
                        return localMachine.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
                    }
                }
            }
            return null;
        }

        public static void Write(string path, string valueName, object value)
        {
            Write(RegistryEnum.LocalMachine, path, valueName, value);
        }

        public static void Write(RegistryEnum registryEnum, string path, string valueName, object value)
        {
            RegistryKey localMachine = null;
            switch (registryEnum)
            {
                case RegistryEnum.LocalMachine:
                    localMachine = Registry.LocalMachine;
                    break;

                case RegistryEnum.ClassesRoot:
                    localMachine = Registry.ClassesRoot;
                    break;

                case RegistryEnum.CurrentConfig:
                    localMachine = Registry.CurrentConfig;
                    break;

                case RegistryEnum.CurrentUser:
                    localMachine = Registry.CurrentUser;
                    break;

                case RegistryEnum.DynData:
                    localMachine = Registry.DynData;
                    break;

                case RegistryEnum.PerformanceData:
                    localMachine = Registry.PerformanceData;
                    break;

                case RegistryEnum.Users:
                    localMachine = Registry.Users;
                    break;
            }
            RegistryKey key2 = localMachine;
            string[] strArray = path.Trim(new char[] { '\\' }).Split(new char[] { '\\' });
            if (!strArray.IsNull() && (strArray.Length != 0))
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    localMachine = localMachine.OpenSubKey(strArray[i], true);
                    if (localMachine.IsNull())
                    {
                        localMachine = key2.CreateSubKey(strArray[i]);
                    }
                    if (i == (strArray.Length - 1))
                    {
                        if (value is string)
                        {
                            localMachine.SetValue(valueName, value.ToString());
                        }
                        else if (((value is uint) || (value is int)) || value.GetType().IsEnum)
                        {
                            if (localMachine.GetValue(valueName, null).IsNull())
                            {
                                localMachine.SetValue(valueName, value, RegistryValueKind.DWord);
                            }
                            else
                            {
                                switch (localMachine.GetValueKind(valueName))
                                {
                                    case RegistryValueKind.DWord:
                                        localMachine.SetValue(valueName, value, RegistryValueKind.DWord);
                                        break;

                                    case RegistryValueKind.Binary:
                                    {
                                        uint num2 = (uint) value;
                                        byte[] buffer = new byte[] { (byte) (num2 & 0xff), (byte) ((num2 & 0xff00) >> 8), (byte) ((num2 & 0xff0000) >> 0x10), (byte) ((num2 & -16777216) >> 0x18) };
                                        localMachine.SetValue(valueName, buffer, RegistryValueKind.Binary);
                                        break;
                                    }
                                    case RegistryValueKind.String:
                                        localMachine.SetValue(valueName, "x" + ((uint) value).ToString("X8"));
                                        break;
                                }
                            }
                        }
                        else if (value is Guid)
                        {
                            localMachine.SetValue(valueName, ((Guid) value).ToString("B"));
                        }
                    }
                    key2 = localMachine;
                }
                if (localMachine.IsNotNull())
                {
                    localMachine.Close();
                }
            }
        }
    }
}

